package com.dwxt.community.service.supervisor;

import com.dwxt.common.base.*;
import com.dwxt.common.util.*;
import com.dwxt.community.dao.*;
import com.dwxt.community.entity.*;
import com.dwxt.community.service.*;
import org.apache.commons.lang3.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

import java.util.*;

/**
 * @program: intelligentcity
 * @description:
 * @author
 * @create: 2020/04/17
 */
@Service
public class RubbishBagRecordService {

    @Autowired
    private UserService userService;
    @Autowired
    private RubbishBagRecordMapper rubbishBagRecordMapper;
    @Autowired
    private SupervisorTaskRecordMapper supervisorTaskRecordMapper;

    //垃圾袋发放
    public BaseResult push(String id, List<String> bagIds) {
        List<RubbishBagRecord> existRubbishBagRecords = rubbishBagRecordMapper.selectByBagIdList(bagIds);
        if (existRubbishBagRecords != null && existRubbishBagRecords.size() > 0) {
            return BaseResult.build(StatusCode.BAG_EXIST_USE, existRubbishBagRecords.stream().map(RubbishBagRecord::getBagId).toArray());
        }
        User user = userService.selectUserByPrimaryKey(id);
        if (user == null) {
            return BaseResult.build(StatusCode.USER_NOT_EXIST);
        }
        //验证地址是否合法
        if (StringUtils.isBlank(user.getCommunityName()) || StringUtils.isBlank(user.getHouseNumber())) {
            return BaseResult.build(StatusCode.HOUSE_ADDR_NULL);
        }
        String supervisorId = PartyUtil.getCurrentUserId();
        String houseAddr = user.getCommunityName() + user.getHouseNumber();
        Date currDate = new Date();
        ArrayList<RubbishBagRecord> rubbishBagRecords = new ArrayList<>();
        RubbishBagRecord rubbishBagRecord;
        for (String bagId : bagIds) {
            rubbishBagRecord = new RubbishBagRecord(IDUtils.genItemId(), bagId, id, supervisorId, houseAddr, currDate, user.getCommunityId(), user.getCommunityName(), user.getHouseNumber());
            rubbishBagRecords.add(rubbishBagRecord);
        }
        int res = rubbishBagRecordMapper.batchInsert(rubbishBagRecords);
        if (res > 0) {
            //新增督导员工作记录记录
            SupervisorTaskRecord supervisorWorkRecord = new SupervisorTaskRecord();
            supervisorWorkRecord.setCreateTime(currDate);
            supervisorWorkRecord.setId(IDUtils.genItemId());
            supervisorWorkRecord.setSupervisorId(supervisorId);
            supervisorWorkRecord.setTaskType("发放垃圾袋");
            supervisorWorkRecord.setUserId(id);
            supervisorTaskRecordMapper.insert(supervisorWorkRecord);
        }
        return BaseResult.ok();
    }

    /**
     * 垃圾袋溯源
     *
     * @param id
     * @return
     */
    public RubbishBagRecord findSource(String id) {
        return rubbishBagRecordMapper.selectByBagId(id);
    }

    /*
     * 获取最后一次
     */
    public Date selectLastGotBagDateByUserId(String id) {
        return rubbishBagRecordMapper.selectLastGotBagDateByUserId(id);
    }
}
